<?php


namespace App\Service;

use GTClient;
use GTPushRequest;
use GTAudienceRequest;
use GTPushMessage;
use GTNotification;
use GTSettings;
use GTStrategy;
use GTPushChannel;
use GTIos;
use GTAps;
use GTAlert;
use GTMultimedia;
use GTAndroid;
use GTUps;
use GTThirdNotification;
use GTRevoke;


class Getui
{
    protected $url;
    protected $appkey;
    protected $appid;
    protected $ms;
    protected $api;
    //protected $mastersecret;

    // https://github.com/GetuiLaboratory/getui-pushapi-php-client-v2
    // 初始化
    public function __construct()
    {
        //$this->mastersecret = '';
        $this->url    = 'https://restapi.getui.com';
        $this->appkey = '';
        $this->appid  = '';
        $this->ms     = '';
        // 初始化操作
        $this->api = new GTClient($this->url, $this->appkey, $this->appid, $this->ms);
    }

    // 向某个客户端发送消息（TODO 测试结果：未测试）
    public function pushByCid($cid, $uid, string $title, string $msg, string $component = '')
    {
        $push = $this->getParam($uid, $title, $msg, $component);
        $push->setCid($cid);
        $res = $this->api->pushApi()->pushToSingleByCid($push);

        return json_encode($res);
    }

    // 创建任务：返回任务id（用于批量推送）
    public function createListMsg($uid, string $title, string $msg, string $component = '')
    {
        $push = $this->getParam($uid, $title, $msg, $component);
        $push->setGroupName("1202test");
        $res = $this->api->pushApi()->createListMsg($push);

        // $taskid = $res['data']['taskid'];

        return json_encode($res);
    }

    // 向某些客户端发送消息（TODO 测试结果：未测试）
    public function pushListByCid(array $cids, $taskid)
    {
        $user = new GTAudienceRequest();
        $user->setIsAsync(true);
        $user->setTaskid($taskid);
        $user->setCidList($cids);
        $res = $this->api->pushApi()->pushListByCid($user);

        return json_encode($res);
    }

    // 向所以客户端发送消息（TODO 测试结果：未测试）
    public function pushAll(string $title, string $msg, string $component = '')
    {
        $push = $this->getParam('all', $title, $msg, $component);
        $push->setGroupName("test");
        $res = $this->api->pushApi()->pushAll($push);

        return json_encode($res);
    }

    // 组合参数
    protected function getParam($uid, string $title, string $msg, string $component = '你的包名/你要打开的 activity 全路径;S.parm1=value1;S.parm2=value2;end')
    {
        $push = new GTPushRequest();
        $push->setRequestId($uid.'_'.micro_time());
        //设置setting
        $set = new GTSettings();
        $set->setTtl(3600000);
        //    $set->setSpeed(1000);
        //    $set->setScheduleTime(1591794372930);
        $strategy = new GTStrategy();
        $strategy->setDefault(GTStrategy::STRATEGY_THIRD_FIRST);
        //    $strategy->setIos(GTStrategy::STRATEGY_GT_ONLY);
        //    $strategy->setOp(GTStrategy::STRATEGY_THIRD_FIRST);
        //    $strategy->setHw(GTStrategy::STRATEGY_THIRD_ONLY);
        $set->setStrategy($strategy);
        $push->setSettings($set);

        // 设置PushMessage，
        $message = $this->setMessage($title, $msg, $component);
        $push->setPushMessage($message);
        $message->setDuration("1590547347000-1590633747000");
        //厂商推送消息参数
        $pushChannel = new GTPushChannel();
        //ios
        $ios = new GTIos();
        $ios->setType("notify");
        $ios->setAutoBadge("1");
        $ios->setPayload("ios_payload");
        $ios->setApnsCollapseId("apnsCollapseId");
        //aps设置
        $aps = new GTAps();
        $aps->setContentAvailable(0);
        $aps->setSound("com.gexin.ios.silenc");
        $aps->setCategory("category");
        $aps->setThreadId("threadId");

        $alert = new GTAlert();
        $alert->setTitle($title);
        $alert->setBody($msg);
        $alert->setActionLocKey("ActionLocKey");
        $alert->setLocKey("LocKey");
        $alert->setLocArgs(array("LocArgs1", "LocArgs2"));
        $alert->setLaunchImage("LaunchImage");
        $alert->setTitleLocKey("TitleLocKey");
        $alert->setTitleLocArgs(array("TitleLocArgs1", "TitleLocArgs2"));
        $alert->setSubtitle("Subtitle");
        $alert->setSubtitleLocKey("SubtitleLocKey");
        $alert->setSubtitleLocArgs(array("subtitleLocArgs1", "subtitleLocArgs2"));
        $aps->setAlert($alert);
        $ios->setAps($aps);

        $multimedia = new GTMultimedia();
        $multimedia->setUrl("url");
        $multimedia->setType(1);
        $multimedia->setOnlyWifi(false);
        $multimedia2 = new GTMultimedia();
        $multimedia2->setUrl("url2");
        $multimedia2->setType(2);
        $multimedia2->setOnlyWifi(true);
        $ios->setMultimedia(array($multimedia));
        $ios->addMultimedia($multimedia2);
        $pushChannel->setIos($ios);
        //安卓
        $android = new GTAndroid();
        $ups     = new GTUps();
        //    $ups->setTransmission("ups Transmission");
        $thirdNotification = new GTThirdNotification();
        $thirdNotification->setTitle("title".micro_time());
        $thirdNotification->setBody("body".micro_time());
        $thirdNotification->setClickType(GTThirdNotification::CLICK_TYPE_URL);
        $thirdNotification->setIntent("intent:#Intent;component=".$component);
        $thirdNotification->setUrl("http://docs.getui.com/getui/server/rest_v2/push/");
        $thirdNotification->setPayload("payload");
        $thirdNotification->setNotifyId(456666);
        $ups->addOption("HW", "badgeAddNum", 1);
        $ups->addOption("OP", "channel", "Default");
        $ups->addOption("OP", "aaa", "bbb");
        $ups->addOption(null, "a", "b");

        $ups->setNotification($thirdNotification);
        $android->setUps($ups);
        $pushChannel->setAndroid($android);
        $push->setPushChannel($pushChannel);

        return $push;
    }

    // 设置消息模板
    protected function setMessage(string $title, string $msg, string $component)
    {
        // 设置PushMessage，
        $message = new GTPushMessage();
        //透传 ，与通知、撤回三选一
        // 设置之：透传
        // $message->setTransmission("试试透传");
        // 设置之一：撤回
        $revoke = new GTRevoke();
        $revoke->setForce(true);
        $revoke->setOldTaskId("taskId");
        //    $message->setRevoke($revoke);

        // 设置之：通知 设置notify
        $notify = $this->setNotify($title, $msg, $component);
        $message->setNotification($notify);

        return $message;
    }

    // 设置通知信息
    protected function setNotify(string $title, string $msg, string $component)
    {
        $notify = new GTNotification();
        //通知
        $notify->setTitle($title);
        $notify->setBody($msg);
        $notify->setBigText("bigTdext");
        //与big_text二选一
        //    $notify->setBigImage("BigImage");

        $notify->setLogo("push.png");
        $notify->setLogoUrl("LogoUrl");
        $notify->setChannelId("Default");
        $notify->setChannelName("Default");
        $notify->setChannelLevel(2);

        $notify->setClickType("none");
        $notify->setIntent("intent:#Intent;component=".$component);
        //$notify->setIntent("intent:#Intent;component=你的包名/你要打开的 activity 全路径;S.parm1=value1;S.parm2=value2;end");
        $notify->setUrl("url");
        $notify->setPayload("Payload");
        $notify->setNotifyId(22334455);
        $notify->setRingName("ring_name");
        $notify->setBadgeAddNum(1);

        return $notify;
    }
}


